import { netWork } from '@/network/request.js';

class Axios { // Axios请求构造类
  constructor () {
  }

  getRequst (url, params = null) { // GET请求函数
    return netWork({
      url,
      method: 'GET',
      params
    })
  }
  
  getPost (url, data = null) { // POST请求函数
    return netWork({
      url,
      method: 'POST',
      data
    })
  }

  getPut (url, params = null, data = null) { // POST请求函数
    // console.log(url, params);
    return netWork({
      url,
      method: 'PUT',
      params,
      data
    })
  }

  getDel (url, params) {
    return netWork({
      url,
      params,
      method: 'DELETE'
    })
  }

}




function getAsideDate () { // Home页面侧边栏请求
  return new Axios().getRequst('/menus');
}

function getUserListData (pageNum, pageSize, query = null) { // user组件请求user所有数据
  return new Axios().getRequst('/user/findUsersByPage', {
    pageNum,
    pageSize,
    query
  });
}

function getUserState (uId, type) { // ElTable组件修改switch组件记录状态
  return new Axios().getPut(`/users/${uId}/state/${type}`);
}

function postAddUserRole (userObj) { // ElDialog组件提交添加成员
  return new Axios().getPost('user/addUser', userObj);
}

function getUserInfo (uid) { // 获取对应id用户的信息
  return new Axios().getRequst(`user/findUserByuid`, {uid});
}

function putUserInfo (id, infoObj) { // 根据id 修改用户的信息(email、mobile)
  return new Axios().getPut(`users/${id}`, null, infoObj);
}

function putUserRole(userId, roleId) { // 分配用户角色
  return new Axios().getPut(`users/${userId}/role`, null, {
    rid: roleId
  })
}

function getUserRoles(id) {
  return new Axios().getRequst(`role/findRolesById`, { id });
}

function getRolePermissions(rid) {
  return new Axios().getRequst(`permission/findPermissionByRid`, { rid });
}

function getAllRoles() {
  return new Axios().getRequst(`role/findAllRoles`);
}

function delUser(uid) {
  return new Axios().getDel(`user/delUser/${uid}`)
}

function updateUser(userObj) {
  return new Axios().getPost(`user/updateUser`, userObj);
}

export {
  getAsideDate,
  getUserListData,
  getUserState,
  postAddUserRole,
  getUserInfo,
  putUserInfo,
  putUserRole,
  getUserRoles,
  getRolePermissions,
  getAllRoles,
  delUser,
  updateUser
}


